/**
 * 警示确认书
 * Created by KY on 2017/11/23.
 */
define(['ldfw/core', 'art', 'text!tpl/product/warn_confirm.tpl',
        'comps/ajax', 'comps/session', 'ldfw/ldlibs/dict','ldfw/ldlibs/browser','ldfw/utils/format',
        'ldfw/helper/formatHelper','jquery-weui'],
    function (LDFW, art, tpl, ajax, session, dict) {
        const View = LDFW.Container.extend({
            className: 'panel',
            tpl: tpl,
            events:{
              'click #recordVideo':'startRecord',
              'change #recordVideo':'uploadRecord',
              'click a[operate="next"]':'next',
              'click a[operate="close"]':'closeVideo'
            },
            initialize:function(){
              this.recordTime = 0
            },
            setPdNo:function(pd_no){
              this.pd_no = pd_no
            },
            startRecord:function(event){
                this.recordTime = new Date().getTime()
            },
            uploadRecord:function(event) {
                let _this = this
                let file = event.target.files[0]
                if(!file){
                    return
                }
                if (browser.versions.ios) {
                    let version = /iPhone\sOS\s([\d_]*)\slike/.exec(navigator.userAgent)[1]
                    version && (version = +version.split('_')[0])
                    // 如果为ISO 10以前的系统 则使用固定文件名判断是否为录像
                    // 如果为ISO 10 则通过文件类型和前缀来判断是否为录像
                    if ((version < 10 && "capturedvideo.MOV" !== file.name) || (version >= 10 && ('video/quicktime' != file.type || file.name.indexOf('trim.') > -1))) {
                        $.alert('不能选择 相册文件，请重新录制')
                        $(event.target).val('')
                        return
                    }
                } else {
                    if (typeof(file.lastModifiedDate) === 'object' && (file.lastModifiedDate instanceof Date)) {
                        let lastModifiedTime = file.lastModifiedDate.getTime()
                        let currentTime = new Date().getTime()
                        if (this.recordTime > lastModifiedTime + 2000 || currentTime < lastModifiedTime - 1000) {
                            $.alert('不能选择相册文件，请重新录制')
                            $(event.target).val('')
                            return
                        }
                    }
                }
                if (file.size>20*1024*1024) {
                    $.alert('您录制的视频过大，请重新录制');
                    $(event.target).val('')
                    return;
                }
                if (file.size<100*1024) {
                    $.alert('您录制的视频过小，请重新录制');
                    $(event.target).val('')
                    return;
                }
                if(file.name){
                    let formData = new FormData()
                    formData.append("file",file)
                    formData.append("relation_field",18)
                    ajax.upload('fileUpload.do',formData).success(function(resp){
                        if(resp.result.length){
                            let fileId = resp.result[0],
                                fileName = '风险不匹配警示确认录像_'+new Date().getTime()+'.mov'
                            _this.file = {
                                fileId:fileId,
                                fileName:fileName
                            }
                            $.toast('上传成功', 1000)
                            let videoUrl = URL.createObjectURL(file)
                            let video = document.getElementById('video')
                            let img = document.createElement("img");
                            video.src=videoUrl
                            // video.addEventListener('loadeddata',function(){
                            //     let canvas = document.createElement("canvas");
                            //     canvas.width = video.videoWidth * 1;
                            //     canvas.height = video.videoHeight * 1;
                            //     canvas.getContext('2d').drawImage(video, 0, 0, canvas.width, canvas.height);
                            //     img.src = canvas.toDataURL("image/png");
                            //     video.poster = img.src
                            // })
                            _this.$el.find('.video-image,.video-select').hide()
                            _this.$el.find('.video').show()
                            _this.$el.find('a[operate="next"]').removeClass('weui-btn_disabled').removeAttr('disabled')

                        }else{
                            $.toast('上传失败','forbidden',1000)
                        }
                    })
                }
                $(event.target).val('')
            },
            closeVideo:function(){
                this.$el.find('.video-image,.video-select').show()
                this.$el.find('.video').hide()
                this.$el.find('a[operate="next"]').addClass('weui-btn_disabled').attr('disabled','disabled')
            },
            next:function(){
                const _this = this
                //修改关联文件
                ajax.post('pfL.9.102',{
                    pre_file_id_str:session.getItem('mismatch_warning_file')?session.getItem('mismatch_warning_file').split(':')[0]+';':'',
                    file_id_str:this.file.fileId+";",
                    file_parent:2
                }).success(function(){
                    //存session
                    session.setItem('mismatch_warning_file',_this.file.fileId+':'+_this.file.fileName)
                })
                //留痕
                ajax.post('cpliL.3.102',{
                    mark_type:12,
                    markinfo_attach:this.file.fileId+':'+this.file.fileName,
                    relation_field:session.getItem('time_stamp'),
                    pd_no:this.pd_no
                }).success(function(){
                    LDFW.navigate('/product/risk_explain/'+_this.pd_no)
                })

            }
        })
        return new View()
    })